package com.wteam.dragon.system.apps.security.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.apps.security.service.deo.OnlineUserDto;
import com.wteam.dragon.system.apps.security.utils.EncryptUtils;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.basebean.vo.PageVo;
import com.wteam.dragon.system.apps.security.service.OnlineUserService;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @Author 陈晓辉
 * @Date 2020/4/2 11:38
 */
@RestController
@RequestMapping("/user/online")
@Api(tags = "系统：在线用户管理")
@RequiredArgsConstructor
public class OnlineController {
    private final OnlineUserService onlineUserService;

    @Log("查询在线用户")
    @ApiOperation("查询在线用户")
    @GetMapping("pageOnLineUser")
    @PreAuthorize("@el.check('pageOnLineUser')")
    public ResultMessage pageOnLineUser(PageVo<OnlineUserDto> pageVo , String filter){
        Page<OnlineUserDto> page = BeanUtil.fromPage(pageVo);
        return onlineUserService.pageOnLineUser(page, filter);
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('download')")
    public void download(HttpServletResponse response, String filter) throws IOException {
        onlineUserService.download(onlineUserService.pageOnLineUser(filter), response);
    }

    @ApiOperation("踢出用户")
    @DeleteMapping("kickOut")
    @PreAuthorize("@el.check('kickOut')")
    public ResultMessage kickOut(@RequestParam("keys") Set<String> keys) throws Exception {
        for (String key : keys) {
            // 解密Key
            key = EncryptUtils.desDecrypt(key);
            onlineUserService.kickOut(key);
        }
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMessage("踢出成功");
        resultMessage.setStatus(HttpStatus.OK.value());
        return resultMessage;
    }
}
